package com.wanxi.servlet.device;

import com.alibaba.fastjson.JSONObject;
import com.wanxi.VO.DeviceVO;
import com.wanxi.dto.ResultDTO;
import com.wanxi.service.DeviceService;
import com.wanxi.service.impl.DeviceServiceImpl;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : 林子淳
 * @create 2023/2/28 8:51
 */
//@WebServlet("/back/device/findAll")
public class DeviceFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //2、获取前端传递后端的值
        String name = req.getParameter("name");
        String normalPrice = req.getParameter("normalPrice");
        String marketPrice = req.getParameter("marketPrice");
        String cuisine = req.getParameter("cuisine");
        String enable = req.getParameter("enable");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");


        //3.封装VO数据
        DeviceVO deviceVO = new DeviceVO();
        deviceVO.setName(Tool.nullToStr(name));
        deviceVO.setNormalPrice(Tool.strToBigDecimal(normalPrice));
        deviceVO.setMarketPrice(Tool.strToBigDecimal(marketPrice));
        deviceVO.setDevice_class_id(Tool.strToInt(cuisine));
        deviceVO.setEnable(Tool.strToInt(enable));
        deviceVO.setPage(Tool.strToPage(page));
        deviceVO.setLimit(Tool.strToLimit(limit));


        System.out.println("进入的了页面："+deviceVO.toString());
               // 4.调用服务逻辑层
        DeviceService productService = new DeviceServiceImpl();
        //        5.得到返回的结果
        ResultDTO resultDTO = productService.findAll(deviceVO);
        System.out.println("DeviceVO返回值："+deviceVO.toString());
        //        6.返回给前端
        resp.getWriter().println(JSONObject.toJSONString(resultDTO));
    }
}
